package com.zhiwang.planet.util

import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import com.tencent.bugly.crashreport.CrashReport

object SystemUtil {

    /**
     * @param context
     * @param packagename
     * @return
     */
    fun isAppInstalled(context: Context, packagename: String): Boolean {
        var packageInfo: PackageInfo?
        try {
            packageInfo = context.packageManager.getPackageInfo(packagename, PackageManager.GET_UNINSTALLED_PACKAGES)
        } catch (e: PackageManager.NameNotFoundException) {
            packageInfo = null
            e.printStackTrace()
        }
        return packageInfo != null
    }


    fun isDownloadManagerAvailable(context: Context): Boolean {
        return try {
            !(context.packageManager
                    .getApplicationEnabledSetting(
                            "com.android.providers.downloads") == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER
                    || context.packageManager
                    .getApplicationEnabledSetting(
                            "com.android.providers.downloads") == PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                    || context.packageManager
                    .getApplicationEnabledSetting(
                            "com.android.providers.downloads") == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED)
        } catch (e: Exception) {
            CrashReport.postCatchedException(Throwable("下载管理器发生错误"))
            false
        }

    }
}  